﻿using System.Collections.Specialized;
using System.Globalization;
using System.Windows.Input;
using Peter.Common;
using Peter.Sdw.Data;

namespace Peter.Sdw.Logic
{
   /// <summary>
   /// View model for passages.
   /// </summary>
   public class PassageModel : BaseModel
   {
      private int m_Order;
      private int m_Number;
      private string m_Text;
      private string m_Header;
      private int m_FooterIndex;
      private bool m_ShowAddFooter;
      private string m_NewFooterText;
      private RelayCommand m_CmdAddFooter;

      /// <summary>
      /// Initializes a new chapter model.
      /// </summary>
      public PassageModel (ChapterModel chapter)
         : base (chapter)
      {
         this.Chapter = chapter;
      }

      #region Passage Data

      /// <summary>
      /// Gets or Sets the id of the passage item.
      /// </summary>
      public int PassageId { get; set; }

      /// <summary>
      /// Gets or Sets the order of the passage.
      /// </summary>
      public int Order
      {
         get { return m_Order; }
         set
         {
            if (this.m_Order != value) {
               this.m_Order = value;
               this.RaisePropertyChanged ("Order");
               this.Edit();
            }
         }
      }

      /// <summary>
      /// Gets or Sets the number of the passage.
      /// </summary>
      public int Number
      {
         get { return m_Number; }
         set
         {
            if (this.m_Number != value) {
               this.m_Number = value;
               this.RaisePropertyChanged ("Number");
               this.Edit ();
            }
         }
      }

      /// <summary>
      /// Gets or Sets the text of the passage.
      /// </summary>
      public new string Text
      {
         get { return m_Text; }
         set
         {
            if (this.m_Text != value) {
               this.m_Text = value;
               this.RaisePropertyChanged ("Text");
               this.Edit ();
            }
         }
      }

      /// <summary>
      /// Gets or Sets the header of the passage.
      /// </summary>
      public string Header
      {
         get { return m_Header; }
         set
         {
            if (this.m_Header != value) {
               this.m_Header = value;
               this.RaisePropertyChanged ("Header");
               this.Edit ();
            }
         }
      }

      #endregion

      /// <summary>
      /// Gets the parent version.
      /// </summary>
      public ChapterModel Chapter { get; private set; }

      /// <summary>
      /// Gets or Sets if we should show or hide the add footer row.
      /// </summary>
      public bool ShowAddFooter
      {
         get { return this.m_ShowAddFooter; }
         set
         {
            this.m_ShowAddFooter = value;
            this.RaisePropertyChanged ("ShowAddFooter");
         }
      }

      /// <summary>
      /// Gets or Sets the footer index.
      /// </summary>
      public int FooterIndex
      {
         get { return this.m_FooterIndex; }
         set
         {
            this.m_FooterIndex = value;
            this.RaisePropertyChanged("FooterIndex");
         }
      }

      /// <summary>
      /// Gets or Sets the footer text.
      /// </summary>
      public string NewFooterText
      {
         get { return this.m_NewFooterText; }
         set
         {
            this.m_NewFooterText = value;
            this.RaisePropertyChanged ("NewFooterText");
         }
      }

      /// <summary>
      /// Gets the command used to add a new footer.
      /// </summary>
      public ICommand AddFooterCommand
      {
         get { return this.m_CmdAddFooter ?? (this.m_CmdAddFooter = new RelayCommand (this.OnAddFooter)); }
      }

      /// <summary>
      /// Posts edit data to the server.
      /// </summary>
      private void Edit ()
      {
         if (!OkToUpdate) return;
         //int entryId, string text, int? order, int? number, string header
         var parameters = new NameValueCollection {
            {Constants.REQUEST_TOKEN, WebQueue.Instance.RequestToken},
            {"entryId", this.Id.ToString (CultureInfo.InvariantCulture)},
            {"order", this.Order.ToString (CultureInfo.InvariantCulture)},
            {"number", this.Number.ToString (CultureInfo.InvariantCulture)},
            {"text", this.Text ?? string.Empty},
            {"header", this.Header ?? string.Empty}
         };
         WebQueue.Instance.Post (Constants.URL_EDIT_PASSAGE, parameters);
      }

      private void OnAddFooter (object obj)
      {

      }
   }
}
